
<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>会员充值</el-breadcrumb-item>
        <el-breadcrumb-item>阅读器盲盒弹窗配置</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      > 
        <template slot-scope="scope" slot="show_numLabel">
          <div style="margin-left:15px">
            <span>客户端展示赠送书币范围 :</span>
          </div>
        </template>
        <template slot-scope="scope" slot="show_numForm">
          <div style="display: none">
            {{form.showstart == null && scope.row.show_num && (form.showstart = (scope.row.show_num.split('-')[0]) )}}
            {{form.showend == null && scope.row.show_num && (form.showend = (scope.row.show_num.split('-')[1]) )}}
          </div>
          <div class="time">
            <el-input :disabled="timedisable" :value="form.showstart" @input="giveinput($event,'showstart')"></el-input>
            <div class="line">———</div>
            <el-input :disabled="timedisable" :value="form.showend" @input="giveinput($event,'showend')"></el-input>
          </div>
        </template>
        <template slot-scope="scope" slot="real_numLabel">
          <div>
            <span>实际赠送书币范围 :</span>
          </div>
        </template>
        <template slot-scope="scope" slot="real_numForm">
          <div style="display: none">
            {{form.realstart == null && scope.row.real_num && (form.realstart = (scope.row.real_num.split('-')[0]) )}}
            {{form.realend == null && scope.row.real_num && (form.realend = (scope.row.real_num.split('-')[1]) )}}
          </div>
          <div class="time">
            <el-input :disabled="timedisable" :value="form.realstart" @input="giveinput($event,'realstart')"></el-input>
            <div class="line">———</div>
            <el-input :disabled="timedisable" :value="form.realend" @input="giveinput($event,'realend')"></el-input>
          </div>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common, updateState } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { appConfigCoinRandom as baseURL,getGroupList,selectVersion } from "@/api/cat/baseUrls";
export default {
  data() {
    // 表单小时分钟的验证
    var validatePass = (rule, value, callback)=>{
      console.log(this.form.hours,'h')
      if (!this.form.hours) {
        callback(new Error('请输入小时'));
        
      } if (!this.form.minutes) {
        callback(new Error('请输入分钟'));
        
      }else {
        callback();
      }
    };
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {
        showstart:null,
        showend:null,
        realstart:null,
        realend:null
      },
      timedisable:false,
      default: {
        orderByList: "create_time&DESC",
      },
      option: { 
        border: true,
        align:'center',
        menuAlign:'center',
        editBtn: true,
        viewBtn: false,
        addBtn: true,
        delBtn: true,
        searchBtn: true,
        refreshBtn: true,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "select",
            span:12,
            placeholder: "请输入APP版本号",
            dicUrl: `${catUrl}/${selectVersion.selectVersion}`,
            props: {
              label: "app_version",
              value: "app_version",
            },
            dicFormatter: (res) => {
              return res.source;
            },
            rules: [
              {
                required: true,
                message: "版本号必须填写",
              },
            ],
          },
           {
            label: "充值金额",
            prop: "price",
            type: "input",
            span:12,
            placeholder: "请输入充值金额"
          },
         
          {
            label: "展示赠币范围",
            prop: "show_num",
            formslot:true,
            labelslot:true,
            span:15,
            labelWidth: '36%'
          },
          {
            label: "实际赠币范围",
            prop: "real_num",
            formslot:true,
            labelslot:true,
            span:15,
            labelWidth: '36%'
          },
          {
            label: "每读n章展示",
            prop: "show_chapter_num",
            placeholder: "请输入赠币",
            span:12,
            labelWidth: '25%',
            searchorder: 4
          }
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        {...this.params,... this.default},
        this.page,
        "POST"
      );

      this.data = response.data.map(item=>{
        item.price = item.price + ''
        item.show_chapter_num = item.show_chapter_num + ''
        item.show_num = item.show_num_start + '-' + item.show_num_end
        item.real_num = item.real_num_start + '-' + item.real_num_end
        return item
      })
      this.page= response.page;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done,loading) {
      row.show_num_start = this.form.showstart
      row.show_num_end = this.form.showend
      row.real_num_start = this.form.realstart
      row.real_num_end = this.form.realend
      row.price = Number(this.form.price)
      row.show_chapter_num = Number(this.form.show_chapter_num)
      this.timedisable = true
      delete row["id"];
      delete row["show_num"];
      delete row["real_num"];
      delete row["showstart"];
      delete row["showend"];
      delete row["realstart"];
      delete row["realend"];
      row.create_time = new Date();
      let userInfo = this.$store.state.user.userInfo
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(baseURL.save, row, null, "POST");
      this.timedisable = false
      if (data==null) {
        loading();
        return;
      }
      
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      row.show_num_start = this.form.showstart
      row.show_num_end = this.form.showend
      row.real_num_start = this.form.realstart
      row.real_num_end = this.form.realend
      row.price = Number(this.form.price)
      row.show_chapter_num = Number(this.form.show_chapter_num)
      this.timedisable = true
      delete row["show_num"];
      delete row["real_num"];
      delete row["showstart"];
      delete row["showend"];
      delete row["realstart"];
      delete row["realend"];
      let userInfo = this.$store.state.user.userInfo
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.edit_time = new Date();
      let data = await common(baseURL.update, row, null, "POST");
      this.timedisable = false
      if (data==null) {
        loading();
        return;
      }
      
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
    
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**搜索 */
    searchChange(params, done) {

      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },

    // 展示和实际赠币范围表单填写
    giveinput(e,type){
      if(!e){
        e = ''
      }
      let _this =this
      _this.$nextTick(() => {
        _this.$set(this.form, type, e);
        _this.$forceUpdate();
      });
    },
  },
};
</script>

<style scoped>
.time {
  display: flex;
}
.timetext {
  width: 100px;
  padding: 0 10px 0 5px;
}

.line {
  margin: 0 10px;
}
</style>